package com.cyq.day19.qq;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MySwing extends JFrame {
    private JButton button;
    private JTextField jTextField;
    private JTextArea jTextArea;

    public MySwing(String name) {
        this.setTitle(name+"的聊天框");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(300, 100, 600, 600);
        this.setLayout(null);
        JTextArea jTextArea = new JTextArea(20, 20);
        JScrollPane jScrollPane=new JScrollPane(jTextArea);
        jScrollPane.setBounds(50,50,400,400);
        this.add(jScrollPane);
        JTextField jTextField = new JTextField(20);
        jTextField.setBounds(50,450,300,50);
        this.add(jTextField);
        JButton button = new JButton();
        button.setBounds(350,450,100,50);
        button.setText("发送");
        this.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jTextField.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "发送消息不能为空");
                } else {
                    SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd 'at' HH:mm:ss z");
                    Date date=new Date(System.currentTimeMillis());
                    jTextArea.append(sdf.format(date) + "\n");
                    jTextArea.append(jTextField.getText() + "\n");
                    jTextArea.setLineWrap(true);
                    jTextArea.setEditable(false);
                }
            }
        });

    }
}
